<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.content" clearable placeholder="名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" v-on:click="handleCurrentChange(1)">查询</el-button>
				</el-form-item>
				<el-form-item class="fr">
					<el-button type="primary" size="medium" plain @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="users" stripe highlight-current-row v-loading="listLoading" border @selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="id" label="ID" min-width="30" align="center"></el-table-column>
			<el-table-column prop="name" label="名称" min-width="80" align="center"></el-table-column>
            <el-table-column label="图片" min-width="80" align="center">
	          <template slot-scope="scope">
			      <el-popover placement="top-start" title="" trigger="click">
			        <img :src="scope.row.url" alt="" style="width: 500px;">
			        <img slot="reference" :src="scope.row.url" style="width: 40px;height: 40px;object-fit: cover;">
			      </el-popover>
			    </template>
	        </el-table-column>
			<el-table-column label="操作" min-width="60" align="center">
				<template slot-scope="scope">
				    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" plain size="mini" @click="handleDels(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<!--<el-button size="small" type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
			<el-pagination background layout="prev, pager, next, jumper" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="名称" prop="name" :rules="[{ required: true, message: '请填写名称', trigger: 'blur' }]" >
					<el-input  v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="图片" prop="url">
                    <el-upload class="avatar-uploader photo" list-type="picture-card" :http-request="(res)=>{return Upload1(res, 'url')}" :multiple="false" :show-file-list="false" action="" :before-upload="beforeAvatarUpload" accept="*/">
					  <img v-if="addForm.url" :src="addForm.url" style="width: 145px;height: 145px;object-fit: cover;border-radius: 5px;">
					  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="addFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="名称" prop="name" :rules="[{ required: true, message: '请填写名称', trigger: 'blur' }]" >
					<el-input  v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="图片" prop="url">
                    <el-upload class="avatar-uploader photo" list-type="picture-card" :http-request="(res)=>{return Upload2(res, 'url')}" :multiple="false" :show-file-list="false" action="" :before-upload="beforeAvatarUpload" accept="*/">
					  <img v-if="editForm.url" :src="editForm.url" style="width: 145px;height: 145px;object-fit: cover;border-radius: 5px;">
					  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="editFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import { client } from '../../common/js/alioss'
	export default {
		data() {
			return {
				filters:{
					content:'',
				},
				sels:[],
				users: [],
				total: 0,
				page: 1,
				pageSize: 10,
				listLoading: false,
				addFormVisible: false,
				addLoading: false,
				addForm: {
					name: '',
					type:'',
					url:'',
					description:'',
				},
				editFormVisible: false,
				editLoading: false,
				editForm: {
					id: '',
					name: '',
					type:'',
					url:'',
					description:'',
				},
			}
		},
		methods: {
			//添加图片
			dialogClose(){
            },
			
			beforeAvatarUpload(file) {
			},//图片上传到阿里云
            Upload1(file, index) {
				var fileName = 'webStorage/web_' + file.file.uid+Math.floor(Math.random()*10000) + file.file.name.substr(file.file.name.indexOf("."));
				client().put(fileName, file.file).then(result => {
					this.addForm[index] = result.url;
					console.log(this.addForm[index]);
				}).catch(function (err) {
                    console.log(err);
	 			});
            },
            Upload2(file, index) {
				var fileName = 'webStorage/web_' + file.file.uid+Math.floor(Math.random()*10000) + file.file.name.substr(file.file.name.indexOf("."));
				client().put(fileName, file.file).then(result => {
					this.editForm[index] = result.url;
				}).catch(function (err) {
                    console.log(err);
	 			});
           },
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//获取列表
			getUsers:function (){
				this.listLoading = true;
				let para ={
					page: this.page,
					limit: this.pageSize,
					content: this.filters.content,
					type:0
				}
				this.$http.post('api/category/list', this.qs.stringify(para)).then(data => {
					let  res = data.data;
					if (res.code !== 200) {
						this.$message({
							message: res.msg,
							type: 'error'
						});
					} else {
  						this.total = res.data.totalNum;
						this.users = res.data.items;
						this.listLoading = false;
					}
				});
			},
			//删除
			handleDels(id) {
				this.$confirm('确认删除吗?', '提示', {}).then(() => {
					this.listLoading = true;
					console.log(id, '================')
					this.$http.post('api/category/removeById', this.qs.stringify({categoryId: id})).then(data => {
						
						let  res = data.data;
						if (res.code !== 200) {
							this.$message({
								message: res.msg,
								type: 'error'
							});
							this.getData();
						} else {
							this.$message({
								message: '成功',
								type: 'success'
							});
							this.getUsers();
						}
					})
				}).catch(() => {

				});
			},
			//批量删除
			batchRemove() {
				var ids = this.sels.map(item => item.id).toString();
				this.handleDels(ids);
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					name: '',
					url:'',
					description:'',
				};
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							let para = {
								type: 0,
								name: this.addForm.name,
								url: this.addForm.url,
								description: this.addForm.description,
								parentId:0,
							};
							this.$http.post('api/category/add', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 200) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.addLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				
				this.editFormVisible = true;
				this.editForm.id = row.id;
				this.editForm.name = row.name;
				this.editForm.url = row.url;
				this.editForm.description = row.description;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = {
								id:this.editForm.id,
								name:this.editForm.name,
								url:this.editForm.url,
								description:this.editForm.description,
							}
							this.$http.post('api/category/update', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 200) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.editLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
		},
		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>